package com.starblink.home.ui.part

import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils.dp2px
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.margin
import com.starblink.android.basic.ext.toastUI
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.util.ImageUtils
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.ActivityStackManage
import com.starblink.home.databinding.FragmentNavHomeBinding
import com.starblink.home.databinding.LayoutHomeBrandBinding
import com.starblink.home.databinding.LayoutHomeBrandItemBinding
import com.starblink.home.ui.NavHomeVM
import com.starblink.home.ui.bean.TopBrandOutletsBean
import com.starblink.home.ui.cells.BrandChildCell
import com.starblink.rocketreserver.FetchBrandCollectionListV2Query

/**
 * 处理折扣
 */
class PartBrand(
    var vb: LayoutHomeBrandBinding,
    var parentVb: FragmentNavHomeBinding,
    var viewModel: NavHomeVM,
    var homeApiResultControl: HomeApiResultControl
) {
    /**
     * 折扣adapter
     */
    private val brandAdapter: MutableAdapter by lazyOnNone {
        MutableAdapter(viewModel.brandAdapterData).apply {
            //类目
            addVhDelegate {
                BrandChildCell(
                    viewModel,
                    parentVb,
                    LayoutHomeBrandItemBinding.inflate(it.inflater, it, false)
                )
            }
        }
    }

    fun handleVM() {
        if (vb.brandRv.adapter == null) {
            var subManager = LinearLayoutManager(ActivityStackManage.currentActivity())
            subManager.orientation = LinearLayoutManager.VERTICAL
            vb.brandRv.layoutManager = subManager
            vb.brandRv.setHasFixedSize(false)
            vb.brandRv.isNestedScrollingEnabled = false
            vb.brandRv.itemAnimator = null
            vb.brandRv.adapter = brandAdapter
        }

        vb.tBrandViewAllTextLay.run {
            trackData(
                page = SpmPageDef.Home080Page_10164,
                element = SpmElementDef.GdsSeemoreWidget,
                onlyHit = true,
                params = mapOf("module" to "brandOutlets")
            )
            click {
                navigationTo(RoutePage.Brand.BRAND_ALL)
            }
        }

    }

    fun dataChange(data: List<FetchBrandCollectionListV2Query.BrandList?>?) {
        if (!data.isNullOrEmpty()) {
            if (data.size > 2) {
                showRecommendTitleCorner(false)
                viewModel.brandAdapterData.clear()
                for ((index, outletsBean) in data.withIndex()) {
                    //最多取前5
                    if (index < 5) {
                        viewModel.brandAdapterData.add(TopBrandOutletsBean(index, outletsBean))
                    }
                }
                brandAdapter.notifyDataSetChanged()

                //处理view All图片集
                handleViewAllImgSet(viewModel.brandLogoList)
            } else {
                showRecommendTitleCorner(true)
            }
        } else {
            showRecommendTitleCorner(true)
        }
    }

    private fun showRecommendTitleCorner(show: Boolean) {
        if (show) {
            parentVb.layRecTitle.clTitleLay.margin(topMargin = dp2px(8f))
            parentVb.layRecTitle.clTitleLay.enableCorner(
                dp2px(24f),
                radiusType = "top"
            )
            parentVb.layBrand.brandLay.gone()
        } else {
            parentVb.layRecTitle.clTitleLay.margin(topMargin = dp2px(0f))
            parentVb.layRecTitle.clTitleLay.enableCorner(dp2px(0f))
            parentVb.layBrand.brandLay.visible()
        }

    }

    //这样写比较好测试
    private val baseInt = 1
    private val baseStartIndex = 0
    private fun handleViewAllImgSet(data: ArrayList<String>) {
        //最少3个显示，最多5个，分别为6,7,8,>8时样式不同
        if (viewModel.brandLogoList.size <= baseInt) {
            vb.lhbImgSet.gone()
            vb.lhbImg1Contain.gone()
            vb.lhbImg2Contain.gone()
            vb.lhbImg3Contain.gone()
        } else if (data.size == baseInt + 1) {
            vb.lhbImgSet.visible()
            vb.lhbImg1Contain.visible()
            vb.lhbImg2Contain.visible()
            ImageUtils.loadImage(
                vb.lhbImg1.lhdiImg.context,
                data[baseStartIndex],
                vb.lhbImg1.lhdiImg
            )
            ImageUtils.loadImage(
                vb.lhbImg2.lhdiImg.context,
                data[baseStartIndex + 1],
                vb.lhbImg2.lhdiImg
            )
        } else if (data.size >= baseInt + 2) {
            vb.lhbImgSet.visible()
            vb.lhbImg1Contain.visible()
            vb.lhbImg2Contain.visible()
            vb.lhbImg3Contain.visible()
            ImageUtils.loadImage(
                vb.lhbImg1.lhdiImg.context,
                data[baseStartIndex],
                vb.lhbImg1.lhdiImg
            )
            ImageUtils.loadImage(
                vb.lhbImg2.lhdiImg.context,
                data[baseStartIndex + 1],
                vb.lhbImg2.lhdiImg
            )
            ImageUtils.loadImage(
                vb.lhbImg3.lhdiImg.context,
                data[baseStartIndex + 2],
                vb.lhbImg3.lhdiImg
            )
            if (viewModel.brandOutletsNum > 8) {
                vb.lhbImg3.lhdiImgCover.visible()
            } else {
                vb.lhbImg3.lhdiImgCover.gone()
            }

        }
    }


}